/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import negocio.CadenaEnteros;
import negocio.MailSender;
import negocio.PasswordGenerator;
import negocio.Usuario;

/**
 *
 * @author JAVA
 */
public class ServletRegistro extends HttpServlet {

    String nombre;
    String apellidos="";
    String sexo;
    String password;
    String email;
    String ciudad;
    int edad;
    int altura;
    String estilo;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String ruta = getServletContext().getContextPath();

        if (request.getParameter("nombre") == null || request.getParameter("apellidos") == null || request.getParameter("ciudad") == null || request.getParameter("e-mail") == null || request.getParameter("sexo") == null || request.getParameter("edad") == null  || request.getParameter("altura") == null || request.getParameter("estilo") == null || !CadenaEnteros.esEntero(request.getParameter("edad")) || !CadenaEnteros.esEntero(request.getParameter("altura"))) {            
            response.sendRedirect(getServletContext().getContextPath()+"/registro.jsp?validacion=error");
        } else {
            
            nombre = request.getParameter("nombre");
            apellidos=request.getParameter("apellidos");
            email = request.getParameter("e-mail");
            edad = Integer.parseInt(request.getParameter("edad"));
            ciudad = request.getParameter("ciudad");
            sexo = request.getParameter("sexo");
            altura = Integer.parseInt(request.getParameter("altura"));
            estilo = request.getParameter("estilo");
            password = PasswordGenerator.getPassword(
                    PasswordGenerator.MINUSCULAS
                    + PasswordGenerator.MAYUSCULAS,10);
            Usuario u = new Usuario(nombre, apellidos, password, email, ciudad, edad, sexo);
            if (altura > 0 && altura < 100) {
                u.setAltura(altura);
            }
            if (estilo != null) {
                u.setEstilo(estilo);
            }

            if (u.existeEmail()) {               
                response.sendRedirect(getServletContext().getContextPath()+"/registro.jsp?error=email");
            } else {
                if (u.guardarUsuario() > 0) {
                    //sesion.setAttribute("usuario", u);
                    //response.sendRedirect(getServletContext().getContextPath()+"/perfil.jsp");                    
                    MailSender.send(u.getEmail(), "Registro en Red Skate", true, "Su contraseña es: " + u.getPassword() + ", recuerda editar el perfil si quiere cambiarla! ");
                    response.sendRedirect(getServletContext().getContextPath() + "/index.jsp?login=ok");
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
